local ClientOHOH = _G.ClientOHOH
local M = ClientOHOH.class.new(script,ClientOHOH.files.OHOHOLYBUIBase())
local ConnectClass = _G.CommonOHOH.load.Connect()
local CallBackManager = ClientOHOH.load.CallBackManager()
local BtnHelper = ClientOHOH.load.BtnHelper()
local TweenService = game:GetService("TweenService")
--获取附属ui列表
local gSubUiList = {}
for _, vScript in pairs(script:GetChildren()) do
    if vScript:IsA("ModuleScript") then
        gSubUiList[vScript.Name] = require(vScript)
    end
end

function M.new(...)
    return M(...)
end

function Initsts(ui)
    ui.insts = {}
    ui.insts.root = "ctrl.insts.root"
    ui.insts.Close = "ctrl.insts.close"
    ui.insts.Scrolling = "ctrl.insts.scrolling"
    ui.insts.selectedColor = "ctrl.SideBar.selectedColor"

end

function SubInitInsts(ui)
    for kName, vScript in pairs(gSubUiList) do
        ui.insts[kName] = vScript.GetInitUiList()
    end
end


function M.ctor(self)
    Initsts(self)
    SubInitInsts(self)
    M.super.ctor(self,ClientOHOH:GetPath_Loop("ReplicatedStorage.Resource.UI.ShopGui"))
    self:MobileSize()
    self.ShopBuyEvent = ConnectClass.new()
    self.CodeExchangeEvent = ConnectClass.new()
    for _, vScript in pairs(gSubUiList) do
        vScript.Init(self)
    end
    self:InitAllButtons()
    self:AddConnect()
end

function M.AddConnect(self)
    CallBackManager:AddRbxConnect(self,self.insts.Close.Activated,function()
		self:Hide()
	end)

    --监听canvasposition的值，来设置当前所在tab
    local length =self.insts.Scrolling.AbsoluteCanvasSize.X
    self.insts.Scrolling:GetPropertyChangedSignal("CanvasPosition"):Connect(function()
        local canvasPosition = self.insts.Scrolling.CanvasPosition/length
        if canvasPosition.X<0.24 then
            self:SetTab("gamepass")
        elseif canvasPosition.X<0.45 then
            self:SetTab("diamond")
        else
            self:SetTab("potions")
        end
    end)

    --进入时可能没有触发ChangedSignal所以需要初始化
    self.BaseShowEvent:Connect(function()
        self:SetTab("gamepass")
    end)
end
function M.MobileSize(self)
    if _G.client.IsMobile then
        if self.insts.root then
            local pos = self.insts.root:GetAttribute("Position")
            local size = self.insts.root:GetAttribute("Size")
            if pos then
                self.insts.root.Position = pos
            end
            if size then
                self.insts.root.Size = size
            end
        end
    end    
end

--初始化所有按钮的基本属性
function M.InitAllButtons(self)
    self:AddProcess(function()
        for _, value in pairs(self.main_inst:GetDescendants()) do
            if value:IsA("GuiButton") then
                BtnHelper:InitButton(value,nil,nil,function() end)
            end
        end
    end)
end

--跳转shop的某一页面
function M.Go(self,tag)
    if not self:IsShowing() then
        self:Show()
    end
    local value = self.insts[tag].gSideTagPos
    
    self:AddProcess(function()
        self:Wait(0.1)
        self:ScrollingGo(value)
    end)
end

function M.ScrollingGo(self,targetX)
    local tweenInfo = TweenInfo.new(0.3,Enum.EasingStyle.Linear)
    local length = self.insts.Scrolling.AbsoluteCanvasSize.X
    local tw = TweenService:Create(self.insts.Scrolling,tweenInfo,{CanvasPosition = Vector2.new(length*targetX,0)})
    tw:Play()
end
--设置tab的激活状态，传入tabName，potions，gamepass，diamond
function M.SetTab(self,tabName)
    local list = {
        potions = false,
        gamepass = false,
        diamond = false
    }
    list[tabName] = true
    for name, value in pairs(list) do
        local ui = self.insts[name]
        if value then
            ui.gSideTag.Parent.BackgroundColor3 =self.insts.selectedColor
        else
            ui.gSideTag.Parent.BackgroundColor3 =Color3.new(0,0,0)
        end
    end
    
end

function M.IsShowing(self)
    return self.main_inst.Enabled
end

function M.Full(self,key,...)
    assert(gSubUiList[key],"不存在UI元素"..key)
    gSubUiList[key].Full(...)
end

function M.Get(self,key,...)
    assert(gSubUiList[key],"不存在UI元素"..key)
    return gSubUiList[key].Get(...)
end

function M.dtor(self)
    M.super.dtor(self)
end


return M